import pygame,time,random
from pygame.locals import *

class Base(object):
    def __init__(self,screen_temp,x,y,image_name):
        self.x = x
        self.y = y
        self.screen = screen_temp
        self.image = pygame.image.load(image_name)

class BasePlane(object):
    def __init__(self,screen_temp,x,y,image_name):
        Base.__init__(self,screen_temp,x,y,image_name)
        self.bullet_list = []
#飞机的类
class HeroPlane(BasePlane):
    def __init__(self,screen_temp):
        BasePlane.__init__(self,screen_temp,200,720,"./feiji/hero1.png")
        #爆炸效果
        self.hit = False
        self.bomb_list = []
        self.__create_images()
        self.image_num = 0
        self.image_index = 0
    def __create_images(self):
        self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n1.png"))
        self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n2.png"))
        self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n3.png"))
        self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n4.png"))
    #将飞机图标导入游戏
    def display(self):
        if self.hit == True:
            self.screen.blit(self.bomb_list[self.image_index], (self.x, self.y))
            self.image_num += 1
            if self.image_num == 7:
                self.image_num = 0
                self.image_index += 1
            if self.image_index > 3:
                time.sleep(1)
                exit()
        else:
            self.screen.blit(self.image,(self.x,self.y))
            for bullet in self.bullet_list:
                bullet.display()
                bullet.move()
                #如果子弹越界了就删掉
                if bullet.judge():
                    self.bullet_list.remove(bullet)
    #向左边动
    def move_left(self):
        self.x-=5
    #向右边动
    def move_right(self):
        self.x+=5
    #飞机发射子弹
    def fire(self):
        self.bullet_list.append(Bullet(self.screen,self.x,self.y))
    def bomb(self):
        self.hit = True
class EnemyPlane(BasePlane):
    def __init__(self,screen_temp):
        BasePlane.__init__(self,screen_temp,0,0,"./feiji/enemy0.png")
        self.direction = "right"
    #将飞机图标导入游戏
    def display(self):
        self.screen.blit(self.image,(self.x,self.y))
        #形成子弹并发射
        for bullet in self.bullet_list:
            bullet.display()
            bullet.move()
            #如果子弹越界了就删掉
            if bullet.judge():
                self.bullet_list.remove(bullet)
    #向左边动
    def move(self):
        if self.direction == "right":
            self.x+=5
        elif self.direction == "left":
            self.x-=5
        if self.x>430:
            self.direction = "left"
        elif self.x<0:
            self.direction = "right"
    #飞机发射子弹
    def fire(self):
        random_num = random.randint(1,100)
        if random_num == 8 or random_num == 80:
            self.bullet_list.append(EnemyBullet(self.screen,self.x,self.y))     
class BaseBullet(Base):
    def __init__(self,screen_temp,x,y,image_name):
        Base.__init__(self,screen_temp,x,y,image_name)
    def display(self):
        self.screen.blit(self.image,(self.x,self.y))
#子弹的类
class Bullet(BaseBullet):
    def __init__(self,screen_temp,x,y):
        BaseBullet.__init__(self,screen_temp,x+40,y-20,"./feiji/bullet.png")
    def move(self):
        self.y -= 5
    #判断子弹是否越界了
    def judge(self):
        if self.y<-2:
            return True
        else:
            return False
class EnemyBullet(BaseBullet):
    def __init__(self,screen_temp,x,y):
        BaseBullet.__init__(self,screen_temp,x+25,y+40,"./feiji/bullet1.png")
    def move(self):
        self.y += 5
    #判断子弹是否越界了
    def judge(self):
        if self.y>838:
            return True
        else:
            return False
#键盘控制
def key_control(hero_temp):
    for event in pygame.event.get():
        #判断是否点击了退出按钮
        if event.type == QUIT:
            print("游戏已退出!")
            exit()
        #判断是否按键
        elif event.type == KEYDOWN:
            #检测按键是否a或者left键
            if event.key == K_a or event.key == K_LEFT:
                print("left")
                hero_temp.move_left()
            #检测按键是否d或者right键
            elif event.key == K_d or event.key == K_RIGHT:
                print("right")
                hero_temp.move_right()
            #检测按键是否空格键
            elif event.key == K_SPACE:
                print("space")
                hero_temp.fire()
            elif event.key == K_b:
                print("b")
                hero_temp.bomb()
def main():
    #创建窗口
    screen = pygame.display.set_mode((480,840),0,32)
    #选择背景图片
    background = pygame.image.load("./feiji/background.png")
    hero = HeroPlane(screen)
    enemy = EnemyPlane(screen)
    #将背景图片给窗口,每隔0.01秒循环一次
    while True:
        #绑定背景图片
        screen.blit(background,(0,0))
        hero.display()
        enemy.display()
        enemy.move()
        enemy.fire()
        #更新游戏窗口
        pygame.display.update()
        key_control(hero)
        time.sleep(0.01)
if __name__=="__main__":
    main()